﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.VisionDemo
{
    public partial class Frm分料装置 : Form
    {
        Plc siemens;
        public Frm分料装置()
        {
            InitializeComponent();
        }

        private void btnPlcConnect_Click(object sender, EventArgs e)
        {
            try
            {


                siemens = new Plc(CpuType.S71200, txtPlcIP.Text, 102, 0, 1);


                // 连接对象
                siemens.Open();
                if (!siemens.IsConnected)
                {
                    labStatus.Text = "连接PLC失败！";
                }
                else
                {
                    labStatus.Text = "连接PLC成功！";
                    //updateTimer.Enabled = true;
                }
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void btnPlcDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                siemens.Close();
                labStatus.Text = "PLC断开连接！";
                //updateTimer.Enabled = false;
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void btn_上升_Click(object sender, EventArgs e)
        {
            siemens.Write("M102.6", true);
        }

        private void btn_下降_Click(object sender, EventArgs e)
        {
            siemens.Write("M102.7", true);
        }

        private void btn_停止_Click(object sender, EventArgs e)
        {
            siemens.Write("M103.0", true);
        }

        private void btn_频率反馈转化_Click(object sender, EventArgs e)
        {
            var value = short.Parse(txt_频率反馈转化.Text);
            siemens.Write("MW400", value);
        }

        private void btn_频率运行设置_Click(object sender, EventArgs e)
        {
            var value = short.Parse(txt_频率运行设置.Text);
            siemens.Write("MW402", value);

        }

        private void btn_减速频率设置_Click(object sender, EventArgs e)
        {
            var value = short.Parse(txt_减速频率设置.Text);
            siemens.Write("MW404", value);

        }

        private void btn_震动器_1_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.0", true);

        }

        private void btn_震动器_2_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.1", true);
        }

        private void btn_震动器_3_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read("M100.2"))
            {
                siemens.Write("M100.2", false);

            }
            else
            {
               siemens.Write("M100.2", true);

            }


        }

        private void btn_震动器_4_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.3", true);

        }

        private void btn_震动器_5_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.4", true);

        }

        private void btn_震动器_6_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.5", true);

        }

        private void btn_震动器_7_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.6", true);

        }

        private void btn_震动器_8_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.7", true);

        }

        private void btn_震动器_9_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.0", true);

        }

        private void btn_震动器_10_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.1", true);

        }

        private void btn_震动器_11_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.2", true);

        }

        private void btn_震动器_12_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.3", true);

        }

        private void btn_震动器_13_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.4", true);

        }

        private void btn_震动器_14_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.5", true);

        }

        private void btn_震动器_15_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.6", true);

        }
    }
}
